using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using KenEgozi.Com.Weblog.Controllers;

namespace KenEgozi.Com.Weblog.Filters
{
	public class AdminOnlyFilter : IFilter
	{
		#region IFilter Members

		///<summary>
		///
		///            Implementors should perform they filter logic and
		///            return 
		///<c>true</c> if the action should be processed.
		///            
		///</summary>
		///
		///<param name="exec">When this filter is being invoked</param>
		///<param name="context">Current context</param>
		///<param name="controller">The controller instance</param>
		///<returns>
		///
		///<c>true</c> if the action 
		///            should be invoked, otherwise 
		///<c>false</c>
		///</returns>
		///
		public bool Perform(ExecuteEnum exec, IRailsEngineContext context, IController controller)
		{
			if (controller is AdminController)
			{
				AdminController adminController = (AdminController)controller;
				if (adminController.Action.Equals("login"))
					return true;
			}

			if (context.Session["IsAdmin"] == null)
			{
				controller.Response.Redirect(null, "admin", "login", DictHelper.Create("returnUrl=" + context.Request.Uri.PathAndQuery));
				return false;
			}
			return true;
		}

		#endregion
	}
}